<template>
	<view class="cart-container" v-if="cart.length !== 0">
		
		<my-address></my-address>
		
		<view class="cart-title">
			<!-- 左侧图标 -->
			<view class="left-pic">
				<uni-icons type="shop" size="24"></uni-icons>
			</view>
			<!-- 右侧文本 -->
			<view class="right-cart">
				<text>购物车</text>
			</view>
		</view>
		
		<!-- 循环渲染购物车中的商品信息 --> 
<!-- 		<block v-for="(item, i) in cart" :key="i">
			<my-goods :item="item" :showRadio="true" :showNum="true" @radio-change="radioChangeHandler" @num-change="numberChangeHandler"></my-goods>
		</block> -->
		
		<!-- 滑动删除 -->
		<uni-swipe-action>
			 <!-- 循环渲染购物车中的商品信息 -->
			<block v-for="(item, i) in cart" :key="i">
				<uni-swipe-action-item :right-options="options" @click="swiperItemClickHanlder(item)">
					<my-goods :item="item" :showRadio="true" :showNum="true" @radio-change="radioChangeHandler" @num-change="numberChangeHandler"></my-goods>
				</uni-swipe-action-item>
			</block>
		</uni-swipe-action>
		
		<!-- 使用自定义结算组件 -->
		<my-settle></my-settle>
		
	</view>
	
	
	<!-- 空白购物车区域 -->
	<view class="empty-cart" v-else>
		<image src="/static/cart_empty@2x.png" class="empty-img"></image>
		<text>空空如也~</text>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex'
	import badgeMix from '@/mixins/tabbar-badge.js'
	
	export default {
		mixins: [badgeMix],
		computed: {
			...mapState('m_cart',['cart'])
		},
		data() {
			return {
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#C00000'
					}
				}]
			};
		},
		methods: {
			...mapMutations('m_cart', ['updateGoodsState', 'updataGoodsCount', 'removeGoodsById']),
			radioChangeHandler(e) {
				this.updateGoodsState(e)
			},
				
			numberChangeHandler(e) {
				this.updataGoodsCount(e)
				
			},
			
			swiperItemClickHanlder(item) {
				this.removeGoodsById(item.goods_id)
				this.setBadge()
				console.log(item.goods_id)
			}
				
		}
	}
</script>

<style lang="scss">
	.cart-title {
		display: flex;
		height: 80rpx;
		align-items: center;
		padding-left: 10rpx;
		border-bottom: 4rpx solid #EFEFEF;
		.right-cart {
			font-size: 32rpx;
			margin-left: 10rpx;
		}
	}
	
	.cart-container {
		padding-bottom: 110rpx;
	}
	
	.empty-cart {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding-top: 350rpx;
		.empty-img{
			height: 400rpx;
			width: 450rpx;
		}
		
		text {
			margin-top: 25rpx;
			color: gray;
		}
	}
</style>
